﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Windows.Forms;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.TeamFoundation.VersionControl.Client;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;

namespace TFSPlus.Commands
{
    public class GetLatestRecursiveForced : IAddinUICommand
    {
        #region IAddinUICommand Members

        public void UISetup(EnvDTE80.DTE2 applicationObject, EnvDTE.AddIn addInInstance, EnvDTE80.Commands2 commands, object[] contextGUIDS)
        {
            Command getLatestRecursiveForcedCommand = commands.AddNamedCommand2(addInInstance, "GetLatestRecursiveForcedSolutionExplorer", "Get Latest Version (Recursive) (Forced)", "Retrieves all items at this item's level and below, even if local version matches the specified version. ", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
            if (getLatestRecursiveForcedCommand != null)
            {
                // Add command to Folder shortcut menu
                Connect.AddCommandToCommandBarMimic(getLatestRecursiveForcedCommand, "Solution", "Get Latest Version");

                // Add command to Project shortcut menu
                Connect.AddCommandToCommandBarMimic(getLatestRecursiveForcedCommand, "Project", "Get Latest Version");

                // TODO : Add to command bar used by other recursive item group types (eg. what type is the "items that 
                //Connect.AddCommandToCommandBarMimic(getLatestRecursiveForcedCommand, "Properties", "Get Latest Version");
            }

        }

        public void Startup(EnvDTE80.DTE2 applicationObject, EnvDTE.AddIn addInInstance)
        {
            
        }

        public EnvDTE.vsCommandStatus QueryStatus()
        {
            if (Connect.ApplicationObject == null || Connect.ApplicationObject.ToolWindows.SolutionExplorer == null)
                return vsCommandStatus.vsCommandStatusInvisible;

            UIHierarchy hierarchy = Connect.ApplicationObject.ToolWindows.SolutionExplorer;
            int count = ((object[])hierarchy.SelectedItems).Length;
            if (count == 1)
                return vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
            else if (count == 0)
                return vsCommandStatus.vsCommandStatusInvisible | vsCommandStatus.vsCommandStatusSupported;
            else
                return vsCommandStatus.vsCommandStatusSupported;
        }

        public void GetItem(object item)
        {
            string filePath = TFSPlusCommon.GetFilePath(item);
            if (String.IsNullOrEmpty(filePath))
                return; // not an item we recognise
            
            string serverPath = Connect.VersionControlExt.SolutionWorkspace.GetServerItemForLocalItem(filePath);

            if (String.IsNullOrEmpty(serverPath))
                return; // not an item in source control path

            Connect.VersionControlExt.SolutionWorkspace.Get(new string[] { serverPath }, VersionSpec.Latest, RecursionType.Full, GetOptions.GetAll | GetOptions.Overwrite);
        }

        static int recursiveDepth = 0;
        public string GetDepthString()
        {
            string s = "";
            for (int i = 0; i < recursiveDepth; i++)
                s += "   ";
            return s;
        }
        public void GetItemsRecursive(UIHierarchyItems uiHierarchyItems)
        {
            recursiveDepth++;
            foreach (UIHierarchyItem item in uiHierarchyItems)
            {
                TFSPlusCommon.OutputGeneralString(GetDepthString() + item.Name + "  - SubItems: " + (item.UIHierarchyItems != null && item.UIHierarchyItems.Count > 0).ToString() + "   Object: " + (item.Object != null).ToString());

                if (item.UIHierarchyItems != null && item.UIHierarchyItems.Count > 0)
                    GetItemsRecursive(item.UIHierarchyItems);
                else if (item.Object != null)
                    GetItem(item.Object);
            }
            recursiveDepth--;
        }


        public void GetItemsRecursive(object[] uiHierarchyItems)
        {
            foreach (UIHierarchyItem item in uiHierarchyItems)
            {
                TFSPlusCommon.OutputGeneralString(GetDepthString() + item.Name + "SubItems: " + (item.UIHierarchyItems != null && item.UIHierarchyItems.Count > 0).ToString() + "   Object: " + (item.Object != null).ToString());

                if (item.UIHierarchyItems != null && item.UIHierarchyItems.Count > 0)
                    GetItemsRecursive(item.UIHierarchyItems);
                else if (item.Object != null)
                    GetItem(item.Object);
            }
        }


        public void Exec()
        {
            if (Connect.VersionControlExt.SolutionWorkspace == null)
                return;

            Window window = Connect.ApplicationObject.ActiveWindow;
            if (window == null)
                return;

            UIHierarchy hierarchy = Connect.ApplicationObject.ToolWindows.SolutionExplorer;
            GetItemsRecursive((object[])hierarchy.SelectedItems);
        }

        public void Disconnect(DTE2 applicationObject, AddIn addInInstance)
        {

        }

        #endregion
    }
}
